package teleradiologia.repository;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import teleradiologia.domain.Diagnosys;
import teleradiologia.domain.Doctor;
import teleradiologia.domain.Ordering;
import teleradiologia.domain.Technician;
import teleradiologia.utils.HibernateUtil;

public class DiagnosysDaoImpl implements DiagnosysDao {

	@Override
	public long create(Diagnosys diagnosys) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		long id = (Long)session.save(diagnosys);

		tx.commit();
		session.close();
		
		return ++id;

	}

	@Override
	public void delete(Diagnosys diagnosys) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(diagnosys);

		tx.commit();
		session.close();
	}

	@Override
	public Diagnosys getById(long id) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Diagnosys diagnosys = (Diagnosys) session.get(Diagnosys.class, id);

		tx.commit();
		session.close();

		return diagnosys;
	}

	@Override
	public void update(Diagnosys diagnosys) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(diagnosys);

		tx.commit();
		session.close();

	}
	@Override
	public Diagnosys getByIdOrdering(long idOrdering) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Diagnosys diagnosys = (Diagnosys) session.createQuery(
				"from Diagnosys diagnosys where diagnosys.ordering.id = :idOrdering").setLong(
				"idOrdering", idOrdering).uniqueResult();
		
		tx.commit();
		session.close();

		return diagnosys;
	}

}
